package no.progark.pattern;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import no.progark.pong.AIController;
import no.progark.pong.Ball;
import no.progark.pong.Controller;
import no.progark.pong.Scoreboard;
import no.progark.views.PongView;

public class PingPongGame extends Game {
	

	Ball ball;
	Controller bottomController;
	AIController topController;
	PingPongGame game;
	
	//for testing
	int fingerPosX = 0;
	int fingerPosY = 0;
	
	int measuredHeight;
	int measuredWidth;
	
	
	Scoreboard scores;
	boolean firstTimeDrawBall = true;
	private int topControllerSpeed = 30;
	boolean won;
	Canvas canvas;
	Context context;
	
	
	public PingPongGame(Context context){
		this.context = context;
		ball = Ball.getInstance(context);
		won = false;
		bottomController = Controller.getInstance(context);
		topController = AIController.getInstance(context);
		scores = Scoreboard.getInstance();
	}

	@Override
	void makePlay(int player) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean endOfGame() {
		if( (scores.getWinner()!=0) ){
			printWinner();
			return true;
		}
		return false;
	}

	@Override
	void printWinner() {
		ball.setVelocity(0);
		 
	}

	@Override
	void initializeGame() {
		// TODO Auto-generated method stub
		
	}
	
	public void onDraw(Canvas canvas){
		if (firstTimeDrawBall) {
			this.canvas = canvas;
			bottomController.initPos(this.getMeasuredWidth()/2, this.getMeasuredHeight()-bottomController.CONTROLLERHEIGHT);
			topController.initPos(this.getMeasuredWidth()/2, 0);
			ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth());
			firstTimeDrawBall = false;
		}
		 
		
		//Set background
		
		
		Rect bcg = new Rect();
		bcg.set(0, 0, canvas.getWidth(), canvas.getHeight());
		
		//Make paint
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.FILL);
		
		//BALL 
		canvas.drawRect(bcg, paint);
		
		
		//Text
		Paint textPaint = new Paint();
		textPaint.setColor(Color.RED); 
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(25);
		
		//canvas.drawText(""+ball.getPosX()+","+ball.getPosY()+"", 50, 50, textPaint);
		//canvas.drawText(""+ fingerPosX + ", " + fingerPosY, 50, 50, textPaint);
		//ManualController
		canvas.drawText(""+scores.getP1() + " : " + scores.getP2(), this.getMeasuredWidth()/2, this.getMeasuredHeight()/2, textPaint);
		if(endOfGame()){canvas.drawText("Winner: player"+ scores.getWinner(), this.getMeasuredWidth()/2-10, this.getMeasuredHeight()/2-20, textPaint);}
		topController.update(ball.getPosX()+(ball.BALLSIZE/2), topControllerSpeed );
		canvas.drawBitmap(bottomController.getImage(), bottomController.getPosX(), bottomController.getPosY(), paint);
		canvas.drawBitmap(topController.getImage(), topController.getPosX(), topController.getPosY(), paint);
		
		canvas.drawBitmap(ball.getImage(), ball.getPosX(), ball.getPosY(), paint);
		if(ball.update(canvas, bottomController, topController)){
			if(ball.getPosY() < 0){
				won = scores.incrementScoreAndCheckWin("p1");
				if(won){
					endOfGame();
				}
				ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth());
			}
			else if(ball.getPosY() > this.getMeasuredHeight()){
				won = scores.incrementScoreAndCheckWin("p2");
				System.out.println("won is " + won);
				if(won){
					endOfGame();
				}
				ball.initPos(getContext(), canvas, this.getMeasuredHeight(), this.getMeasuredWidth());
			}
		}
	
	}
	
	public Context getContext(){
		return context;
	}
	
	public int getMeasuredHeight(){
		return measuredHeight;
	}
	public int getMeasuredWidth(){
		return measuredWidth;
	}
	
	public void setMeasuredHeight(int m){
		measuredHeight = m;
	}
	public void setMeasuredWidth(int m){
		measuredWidth = m;
	}
	
	public void onTouch(MotionEvent event){
		bottomController.update((int) event.getX());
	}
	
	public void reset(){
		won = false;
		scores.setP1(0);
		scores.setP2(0);
		ball.setVelocity(7);
	}

}
